var app = require('express')()
var server = require('http').Server(app)
var io = require('socket.io')(server)

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/public/index.html')
})

io.of('/chat-room').on('connection', client => {
    console.log('connected')
    client.on('client-message', ({ userInfo, id, date, message }) => {
        //  只有自己可以看到
        client.emit('server-message', { userInfo, id, date, message })
        // 只有其他人可以看到
        client.broadcast.emit('server-message', {
            userInfo,
            id,
            date,
            message
        })
    })
})

const port = 8088
io.listen(port)
console.log('listening')
